package logique.reseau;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;

import controleur.Controleur;

/**
 * 
 * @author camille
 *
 */
public class Reseau {
	private Controleur controleur;

	private String id;
	private Hashtable<String, Route> routes;
	private Hashtable<String, Ville> villes;

	public Reseau(Controleur controleur) {
		this.controleur = controleur;
		id = null;
		routes = new Hashtable<String, Route>();
		villes = new Hashtable<String, Ville>();
	}

	public Controleur getControleur() {
		return controleur;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void addRoute(Route route) throws ReseauException {

		if (routes.get(route.getNom()) != null) {
			throw new ReseauException("Une route portant le meme nom existe deja");
		}

		this.routes.put(route.getNom(), route);
	}

	public Hashtable<String, Route> getRoutes() {
		return routes;
	}

	public void setRoutes(Hashtable<String, Route> routes) {
		this.routes = routes;
	}

	public void addVille(Ville ville) throws ReseauException {

		if (villes.get(ville.getNom()) != null) {
			throw new ReseauException("Une ville portant le meme nom existe deja");
		}

		this.villes.put(ville.getNom(), ville);
	}

	public Hashtable<String, Ville> getVilles() {
		return villes;
	}

	public void setVilles(Hashtable<String, Ville> villes) {
		this.villes = villes;
	}

	@Override
	public String toString() {
		String reseauStr = new String();

		reseauStr += "\n################################################\n";
		reseauStr += "Reseau " + id + " :\n";
		reseauStr += "\n################################################\n";

		reseauStr += "Villes :\n";
		reseauStr += "---------------\n";

		for (Iterator<Entry<String, Ville>> iterator = villes.entrySet().iterator(); iterator.hasNext();) {

			Entry<String, Ville> entry = iterator.next();

			reseauStr += "\t" + entry.getValue().toString() + "\n";
		}

		reseauStr += "\nRoutes :\n";
		reseauStr += "---------------\n";

		for (Iterator<Entry<String, Route>> iterator = routes.entrySet().iterator(); iterator.hasNext();) {
			Entry<String, Route> entry = iterator.next();
			reseauStr += "\t" + entry.getValue().toString() + "\n";
		}

		reseauStr += "\n################################################\n";

		return reseauStr;
	}

}
